// JavaScript Document
Vue.component('config-comp',{
	template:' <div><dt>\
                	<span>{{room.name}}</span>\
                    <em>共<i>{{total}}</i>件，<i>{{totalPrice}}</i>元</em>\
                    <b v-on:click="removeItem()"></b>\
                </dt>\
                <dd v-show="room.show">\
					<div class="li" v-for="(item,index) in room.items">\
						<item-comp v-bind:item="item" v-bind:index="index" v-on:changeitem="changeItem"></item-comp>\
					</div>\
                </dd></div>',
				props:['room','rid'],
				data:function(){
					return {
						types:[
							{name:'床',img:'img/sf_ico.png'},
							{name:'沙发',img:'img/sf_ico.png'},
							{name:'餐桌',img:'img/sf_ico.png'},
							{name:'椅子',img:'img/sf_ico.png'},
							{name:'茶几',img:'img/sf_ico.png'},
							{name:'自定义',img:'img/zdy_ico.png'}
						]
					}
				},
				computed:{
					total:function(){
						var count = 0;
						for(var i=0;i<this.room.items.length;i++){
							var item = this.room.items[i];
							if(!!item.count){
								count += item.count;	
							}	
						}
						return count;
					},
					totalPrice:function(){
						var count = 0;
						for(var i=0;i<this.room.items.length;i++){
							var item = this.room.items[i];
							if((!!item.price)&& (!!item.count)){
								count += item.price*item.count;	
							}	
						}
						return count;
					}
				},
				methods:{
					/**
					 * 删除配置单
					 */
					removeItem:function(){
						for(var i=0;i<this.room.items.length;i++){
							this.room.items[i].name = this.room.items[i].defaultName;
							this.room.items[i].id = this.room.items[i].defaultId;
							this.room.items[i].img=this.room.items[i].defaultImg;
							this.room.items[i].count=0;
							this.room.items[i].spcificationId='';
							
							//var type ;
							/*
							for(var j=0;j<this.types.length;j++){
								if(this.types[j].name == this.room.items[i].type){
									type  = this.types[j];
									break;
								} 
							}
							*/
							//this.room.items.splice(i,1,{type:type.name,img:type.img,count:0,id:'',name:'',price:0});
						}
					},
					addItem:function(){
						this.room.items.push({type:'沙发',img:'img/sf_ico.png',count:0,id:'',name:'',price:0});	
					},
					showTypes:function(event){
						if (event) event.preventDefault()
						$(this.$el).find('ul').show();
					},
					hideTypes:function(){
						$(this.$el).find('ul').hide();
					},
					addType:function(name,event){
						for(var i=0;i<this.types.length;i++){
							if(this.types[i].name == name){
								this.room.items.push({type:name,img:this.types[i].img});
							}
						}
						this.hideTypes();
						if (event) event.preventDefault()
					},
					changeItem:function(index){
						this.$emit('changeitem',index,this.rid);
					}
				}
});